package com.zzy.demo.config.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 数据拦截器
 * 
 * @author guokaige
 *
 */
@Component
public class DataInterceptor implements HandlerInterceptor {

	private static final String REDIRECT_API = "redirect";
	
	private static final String QINIU_BASE_URI = "http://qn.kaiweiwangluo.cn";

	

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		if (null != modelAndView && null != modelAndView.getViewName()) {

			String viewName = modelAndView.getViewName().toUpperCase();

			if (!viewName.startsWith(DataInterceptor.REDIRECT_API.toUpperCase())) {
				// 得到当前action的请求路径
				Map<String, Object> model = modelAndView.getModelMap();
				model.put("base", request.getContextPath());
				model.put("qnBaseUri", DataInterceptor.QINIU_BASE_URI);
			}
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}